<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class VideoSpider extends Base
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $id = input('id');
        $app_set = Db::name('videospider_set')->find();
        if (IS_POST) {
            $post_data = input();
            $data = [];
            $data['free_num'] = $post_data['free_num'];
            $data['pay_num'] = $post_data['pay_num'];
            $data['pay_num_month'] = $post_data['pay_num_month'];
            $data['bgcolor'] = $post_data['bgcolor'];
            $data['create_time'] = time();
            if($id){
                $row = Db::name('videospider_set')->where(array('id' => $id))->update($data);
            }else{
                $row = Db::name('videospider_set')->insertGetId($data);
            }

            return json(['status'=>1,'msg'=>'修改成功','url'=>url('VideoSpider/index')]);
        }
        View::assign('info', $app_set);
        return View::fetch();
    }
    public function category_lists(){
        $clist = Db::name('videospider_category')->where('1=1')->column('name','id');
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('name')){
                $map['a.name'] = input('name');
            }
            $count = Db::name('videospider_category')->alias('a')->where($map)->count();

            $field = '*';
            $list = Db::name('videospider_category')
                ->alias('a')
                ->field($field)
                ->where($map)
                ->order('id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        View::assign('clist',$clist);
        return View::fetch();
    }
    public function edit_category(){
        if(IS_POST){
            $info = input();
            if(!empty($info['id'])){
                Db::name('videospider_category')->where('id',$info['id'])->update($info);
            }else{
                $data = [];
                $data['name'] = $info['name'];
                $data['pic'] = $info['pic'];
                $data['sort'] = $info['sort'];
                $info['w_time'] = time();
                $id = Db::name('videospider_category')->insertGetId($info);
            }
        }else{
            $id = input('id');
            $info = Db::name('videospider_category')->where('id',$id)->find();
            View::assign('info',$info);
            return View::fetch();
        }
    }
}